package com.qxp.simplerpc.registry;

import com.qxp.simplerpc.RegistryService;
import com.qxp.simplerpc.connect.RpcClient;
import com.qxp.simplerpc.exception.RpcException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Objects;
@Component
public class Subscribe {

    private ApplicationContext applicationContext;
    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    @PostConstruct
    private void subscribe(){
        RegistryService registryService = this.applicationContext.getBean(RegistryService.class);
        RpcClient rpcClient = this.applicationContext.getBean(RpcClient.class);
        if(Objects.isNull(registryService)){
            throw new RpcException("RegistryService instance can't be null...please check again...");
        }
        registryService.subscribe(rpcClient.getRpcClientHandler());
    }
}
